
def dynamic_squares(board , player):
    weights = get_dynamic_weights(board)
    if board.game_over():
        return terminal_value(board,player)
    l = map(lambda a,b: a*b, board.flatten(), weights)

    l = [ x*y for x, y in zip(board.flatten(), weights) ]
    
    v = sum(l) * player
    return v

def get_dynamic_weights(board):
    # Define coordinates for the corners
    c1 = board.create_move(0,0)
    c2 = board.create_move(0,7)
    c3 = board.create_move(7,7)
    c4 = board.create_move(7,0)
    # Define dictionaries for spaces
    near_corner = { 0:-100, 1:5, -1:-5 }
    # Edge dict gets passed (# of my corners near edge, # of his corners near edge)
    edge = { (0,0):3, (1,0):5, (2,0):10, (0,1):1, (0,2):10, (1,1):10  }

    # Calculate weights for squares near corner
    D1 = near_corner[board.get(c1)]
    D2 = near_corner[board.get(c2)]
    D3 = near_corner[board.get(c3)]
    D4 = near_corner[board.get(c4)]
    # Calculate weights for edges
    def get_edge(board, corner1, corner2):
        # Returns ( # of my, # of his ) corners given 2 corners
        corners = [ board.get(corner1) , board.get(corner2) ]
        return (corners.count(1), corners.count(-1) )
    Ea = edge[get_edge(board, c1, c2)]
    Eb = edge[get_edge(board, c2, c3)]
    Ec = edge[get_edge(board, c3, c4)]
    Ed = edge[get_edge(board, c4, c1)]

    # Static weights
    C1 = C2 = C3 = C4 = 1000
    N = 2
    weights =  [C1,D1,Ea,Ea,Ea,Ea,D2,C2,
                D1,D1,N ,N ,N ,N ,D2,D2,
                Ed,N ,N ,N ,N ,N ,N ,Eb,
                Ed,N ,N ,N ,N ,N ,N ,Eb,
                Ed,N ,N ,N ,N ,N ,N ,Eb,
                Ed,N ,N ,N ,N ,N ,N ,Eb,
                D4,D4,N ,N ,N ,N ,D3,D3,
                C4,D4,Ec,Ec,Ec,Ec,D3,C3]
    return weights

def terminal_value(board,player):
    if board.piece_count(player) > 0:
        return 100000
    elif board.piece_count(player) < 0:
        return -100000
    else:
        return 0


if __name__=='__main__':
    from othello import *
    board = OthelloBoard()
    board.setup_board()
    print board
    print dynamic_squares(board,WHITE)
    board.put(Move(1,0,WHITE))
    print dynamic_squares(board,WHITE)
    board.put(Move(0,0,WHITE))
    print dynamic_squares(board,WHITE)